
#include <stdio.h>
#include "uart.h"
#include "device.h"

static uart_t *console_uart = NULL;

int _write (int fd, char *pBuffer, int size)
{
	if(fd > 2) return EOF;

	if(!console_uart) return 0;

	int ret = console_uart->write((uint8_t *)pBuffer, size);

	if(ret <= 0) size = 0;
	
    return size;
}

int putchar(int c)
{
	if(!console_uart) return EOF;

	uint8_t chr = (uint8_t)c;
	int ret = console_uart->write(&chr, 1);
	if(ret != 0) return EOF;

	return c;
}

uart_t *console_init(uart_port_e port, uart_cfg_t *cfg)
{
	if(console_uart) return console_uart;

	console_uart = uart_find_by_port(port);
	
	if(console_uart)
		console_uart->open(cfg);
	
	return console_uart;
}

uart_t *console_get_dev(void)
{
	return console_uart;
}